<?php
// deal with sql
require_once 'common.php';
require_once 'MJDMTask.php';
require_once 'MJDMUser.php';
require_once 'MJDMReward.php';

function DB_FetchUserRow($uid)
{
	$query = "select * from user where id = $uid ";
	
	$row = executeRow($query);
	
	return $row;
}

function DB_FetchTaskList($uid)
{
	$query ="select * from task where uid = $uid and finished = 0 order by dueDate";
	$result = excuteMySQLCmd($query);
	
	return $result;
}

function DB_FetchRewardList($uid)
{
	$query ="select * from reward where uid = $uid and finished = 0";
	$result = excuteMySQLCmd($query);
	
	return $result;
}

function DB_InsertTask($task)
{
	$query = "INSERT INTO task(`id`,`name` ,`points` ,`type` ,`uid` ,`dueDate`,tag, createDate,url) 
	VALUES (NULL ,  '$task->name',  '$task->points',  '$task->type',  $task->uid, '$task->dueDate', '$task->tag',Now(), '$task->url' ) ";
	
	executeNonQuery($query);
	return true;
}

function DB_UpdateTask($task)
{
	$query ="UPDATE task SET `name` = '$task->name' ,`points` =$task->points  ,`type` = '$task->type' ,
	`dueDate` = '$task->dueDate',`tag` = '$task->tag' , `url` = '$task->url' WHERE id = $task->id";
	
	executeNonQuery($query);
	return true;
}

function DB_DeleteTask($id)
{
	$query = "DELETE FROM task where id = $id ";
	
	executeNonQuery($query);
	return true;
}

function DB_FinishTask($id)
{
	$query ="UPDATE task SET finished = 1,finishedDate = Now() WHERE id = $id ";
	
	executeNonQuery($query);
	return true;
}

function DB_InsertReward($reward)
{
	$query = "INSERT INTO reward(`id`,`name` ,`points` ,`uid` ,createDate,url)
	VALUES (NULL ,  '$reward->name',  '$reward->points',  $reward->uid,  Now(), '$reward->url') ";
	
	executeNonQuery($query);
	return true;
}

function DB_UpdateReward($reward)
{
	$query ="UPDATE reward SET `name` = '$reward->name' ,`points` = $reward->points,url = '$reward->url' WHERE id = $reward->id";
	
	executeNonQuery($query);
	return true;
}

function DB_DeleteReward($id)
{
	$query = "DELETE FROM reward where id = $id ";
	
	executeNonQuery($query);
	return true;
}

function DB_WonReward($id,$points)
{
	$query ="UPDATE reward SET finished = 1,finishedDate = Now() WHERE id = $id ";
	
	executeNonQuery($query);
	return true;
}

function DB_UpdateUserPoints($uid,$points)
{
	$query = "update user set earnpoints = earnpoints + $points where id = $uid";
	
	executeNonQuery($query);
	return true;
}



?>